package org.ftz.control;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
public class EventThread implements Runnable, Serializable { //用来处理接收过来的鼠标事件或者键盘事件
    public ObjectInputStream ois;
    public Robot robot;
    InputStream stream;
    public EventThread(InputStream is) {
        try {
            this.stream = is;
            this.ois = new ObjectInputStream(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        try {
            robot = new Robot();
            while (true) {
                Object o = null;
                o = ois.readObject();
                if (o instanceof MouseWheelEvent) {
                    mouseWheelEvent((MouseWheelEvent) o);
                } else if (o instanceof KeyInfo) {
                    keyEvent((KeyInfo) o);//处理鼠标滚轮事件
                } else if (o instanceof MouseEvent) {
                    mouseEvent((MouseEvent) o);//处理鼠标事件
                }
            }
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void mouseEvent(MouseEvent e) { //鼠标事件
        int type = e.getID();
        if (type == Event.MOUSE_MOVE) {//鼠标移动草走
            robot.mouseMove(e.getX(), e.getY());
        } else if (type == Event.MOUSE_DOWN) {//鼠标按下操作
            type = getMouseKey(e.getButton());
            robot.mousePress(type);
        } else if (type == Event.MOUSE_UP) {//鼠标按上来操作
            robot.mouseRelease(getMouseKey(e.getButton()));
        } else if (type == Event.MOUSE_DRAG) {//鼠标按住移动操作
            int x = e.getX();
            int y = e.getY();
            robot.mouseMove(x, y);//鼠标拖动
        }
    }
    public void mouseWheelEvent(MouseWheelEvent e) { //滚轮事件
        if (e.getPreciseWheelRotation() > 0) {//鼠标向上滑动
            robot.mouseWheel(1);
        } else {//鼠标向下滑动
            robot.mouseWheel(-1);
        }
    }
    public void keyEvent(KeyInfo info) { //键盘事件
        int type = info.getEvent();//拿到事件类型
        if (type == Event.KEY_PRESS) {
            robot.keyPress(info.getKey_code());
        } else if (type == Event.KEY_RELEASE) {
            robot.keyRelease(info.getKey_code());
        }
    }
    public int getMouseKey(int button) { //返回鼠标的真正事件，鼠标时间不能直接处理，需要进过转换
        if (button == MouseEvent.BUTTON1) {//鼠标左键
            return InputEvent.BUTTON1_MASK;
        } else if (button == MouseEvent.BUTTON2) {//鼠标右键
            return InputEvent.BUTTON2_MASK;
        } else if (button == MouseEvent.BUTTON3) {//滚轮
            return InputEvent.BUTTON3_MASK;
        } else {
            return 0;
        }
    }
}
